/**   
 * @Title: RegistWebAct.java 
 * @Package com.esun.rabbit2.viewer 
 * @Description: 注册用webview activity 
 * @author: Billy Qigs@500wan.com   
 * @date: 2012-7-17 上午10:10:32  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer;

import org.androidpn.client.Constants;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.config.Key;
import com.esun.ui.R;
import com.esun.util.log.EDebug;
import com.esun.util.magic.magic;

/**
 * @ClassName: RegistWebAct
 * @Description: 注册用webview activity
 * @author: Billy Qigs@500wan.com
 * @date: 2012-7-17 上午10:10:32
 * 
 */
public class RegistWebAct extends Activity {

	/**
	 * @Fields btnBack : 返回按钮
	 */
	private ImageButton btnBack;

	/**
	 * @Fields webRegist : 注册页面用webview
	 */
	private WebView webRegist;

	/**
	 * @Fields relativeProgress : 加载过程界面
	 */
	private RelativeLayout relativeProgress;

	private String versionName;
	private String url;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist_web_act);
		initUI();
		webRegist.loadUrl(url);
	}

	private void initUI() {

		btnBack = (ImageButton) findViewById(R.id.registwebact_imgbtn_back);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(RegistWebAct.this, LoginAct.class));
				RegistWebAct.this.finish();
			}
		});

		relativeProgress = (RelativeLayout) findViewById(R.id.registwebact_relative_progress);

		webRegist = (WebView) findViewById(R.id.registwebact_webview_regist);
		webRegist.getSettings().setJavaScriptEnabled(true);
		webRegist.setBackgroundColor(0);
		webRegist.setScrollBarStyle(ScrollView.SCROLLBARS_OUTSIDE_OVERLAY);

		String channel = DistributionChannels.QUDAO;
		String platform = Config.NUM1;
		SharedPreferences sharedPre = this.getSharedPreferences(Config.CLIENT_PREFERENCES, Context.MODE_PRIVATE);
		String userIMEI = sharedPre.getString(Constants.DEVICE_ID, Config.DEFAULT_LEAGUAGE_NUM + "_61");
		String useragent = android.os.Build.MODEL;
		String IMEI = magic.getIMEI(RegistWebAct.this);
		try {
			versionName = this.getPackageManager().getPackageInfo(Config.APP_PACKAGE_INFO, 0).versionName;
		} catch (NameNotFoundException e) {
			versionName = Config.NUM0;
		}
		url = Config.ACTIVITY_REGIST_URL + "?channel=" + channel + "&platform=" + platform + "&version=" + versionName + "&userimei=" + userIMEI + "&useragent=" + useragent + "&mbimei=" + IMEI;
		EDebug.o(this, "regist url = " + url);
		webRegist.setWebChromeClient(new WebChromeClient() {
			@Override
			public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
				Log.d("TAG", "alert");
				new AlertDialog.Builder(RegistWebAct.this).setTitle("").setMessage(message).setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						result.confirm();
					}

				}).setCancelable(false).create().show();
				return true;
			}

		});
		webRegist.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				relativeProgress.setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				relativeProgress.setVisibility(View.VISIBLE);
			}
		});

		// js调用函数 实现客户端自动登录以及退出activity
		webRegist.addJavascriptInterface(new Object() {
			@SuppressWarnings("unused")
			public void login(final String username, final String userpwd, final String mobile) {

				relativeProgress.setVisibility(View.VISIBLE);
				Intent dataIntent = new Intent();
				Bundle dataBundle = new Bundle();
				dataBundle.putString(Key.KEY_USERNAME, username);
				dataBundle.putString(Key.KEY_PASSWORD, userpwd);
				dataBundle.putString(Key.KEY_MOBILENUM, mobile);
				dataIntent.putExtras(dataBundle);
				setResult(100, dataIntent);
				RegistWebAct.this.finish();

			}
		}, "login");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webRegist.canGoBack()) {
				webRegist.goBack();
			} else {
				setResult(109);
				RegistWebAct.this.finish();
			}
		}

		return true;
	}

}
